/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ShapeDrawbleZG.java   
 * @Package com.swift.maintenance.utils   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-7-6 上午10:39:20   
 * @version V1.0     
 */
package com.swift.maintenance.utils;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.graphics.Paint.Join;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewTreeObserver.OnPreDrawListener;

/**
 * 生成圆角矩形背景
 * 
 * @author Administrator
 * 
 */
public class ShapeDrawbleZG extends Drawable {
	private static final int DEFAULT_WIDHT = 0;
	private static final int DEFAULT_HEIGHT = 0;
	private static final int DEFAULT_EDHWIDTH = 5;
	private Paint p;
	private Paint innerP;
	private int width = DEFAULT_WIDHT;// 背景宽度
	private int height = DEFAULT_HEIGHT;// 背景高度
	private int r;// 圆角半径
	private boolean isEdging = false;// 是否带边框
	private int edgWidth = DEFAULT_EDHWIDTH;// 边框厚度

	public ShapeDrawbleZG(View view, int radius, boolean isEdg) {
		initPaint();
		getViewWH(view);
		r = radius;
		isEdging = isEdg;
	}

	private void initPaint() {
		p = new Paint();
		p.setColor(Color.YELLOW);
		p.setStrokeJoin(Join.ROUND);
		p.setStrokeCap(Cap.ROUND);
		p.setStrokeWidth(3);
		innerP = new Paint();
		innerP.setColor(Color.WHITE);
		innerP.setStrokeJoin(Join.ROUND);
		innerP.setStrokeCap(Cap.ROUND);
		innerP.setStrokeWidth(3);
	}

	boolean hasMeasured = false;

	private void getViewWH(final View v) {
		if (null == v) {
			return;
		}
		v.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

			@Override
			public boolean onPreDraw() {
				if (!hasMeasured) {
					width = v.getMeasuredWidth();
					height = v.getMeasuredHeight();
					hasMeasured = true;
				}
				return true;
			}
		});
	}

	public ShapeDrawbleZG setRadius(int radius) {
		this.r = radius;
		invalidateSelf();
		return this;
	}

	public ShapeDrawbleZG setEdgWidth(int width) {
		this.edgWidth = width;
		if (isEdging) {
			invalidateSelf();
		}
		return this;
	}

	public ShapeDrawbleZG setEdgColor(int color) {
		p.setColor(color);
		invalidateSelf();
		return this;
	}

	public ShapeDrawbleZG setInnerColor(int color) {
		innerP.setColor(color);
		invalidateSelf();
		return this;
	}

	private void drawRect(Canvas canvas) {
		RectF rf = new RectF(0, 0, width, height);

		canvas.drawRoundRect(rf, r, r, p);
	}

	private void drawEdg(Canvas canvas) {
		RectF rf = new RectF(edgWidth, edgWidth, width - edgWidth, height
				- edgWidth);
		canvas.drawRoundRect(rf, r, r, innerP);
	}

	@Override
	public void draw(Canvas canvas) {
		drawRect(canvas);
		if (isEdging) {
			drawEdg(canvas);
		}
	}

	@Override
	public void setAlpha(int alpha) {
		// TODO Auto-generated method stub
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
		// TODO Auto-generated method stub
	}

	@Override
	public int getOpacity() {
		// TODO Auto-generated method stub
		return PixelFormat.UNKNOWN;
	}

}
